package com.printeverywhere.action;

import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.printeverywhere.delegator.PromotionalCodesServiceDelegator;
import com.printeverywhere.dto.PromotionalCode;
import com.printeverywhere.util.AdminConstants;
import com.printeverywhere.util.CommonUtil;

public class PromotionalCodesAction extends AdminSystemSetup {

	/**
	 *
	 */
	private static final long serialVersionUID = 1L;

	private List<PromotionalCode> promotionalCodeList;
	private PromotionalCode promotionalCode;
	private Map<Integer , PromotionalCode> promotionalCodeMap;
	private Logger logger = Logger.getLogger(PromotionalCodesAction.class);

	@Override
	public String execute() throws Exception {
		try{
			this.promotionalCode = new PromotionalCode();
			getList();
		}catch (Exception e) {
			logger.info("Error in displaying list.".intern());
			addActionError("Error in displaying list.".intern());
			return INPUT;
		}
		return SUCCESS;
	}
	public static void main(String[] args) {
		PromotionalCodesServiceDelegator.getList();
	}

	public String addPromotionalCodes(){
		try{
			if(this.promotionalCode.getMoney() != null){
				this.promotionalCode.setPromotionalCode(CommonUtil.getRandomValue(AdminConstants.EIGHT_NUMBER));
				PromotionalCodesServiceDelegator.add(this.promotionalCode);
				addActionMessage("Promotional code successfully added.");
			}else{
				addActionError("Please, enter integer value for money.".intern());
				return INPUT;
			}
		} catch (NumberFormatException e) {
			logger.info("Record not added.".intern());
			addActionError("Enter integer value for money.".intern());
			return INPUT;
		} finally {
			getList();
		}
		return SUCCESS;
	}
	@Override
	public void validate() {
		getList();
	}
	private void getList(){
		promotionalCodeList = PromotionalCodesServiceDelegator.getList();
	}

	public List<PromotionalCode> getPromotionalCodeList() {
		return promotionalCodeList;
	}

	public void setPromotionalCodeList(List<PromotionalCode> promotionalCodeList) {
		this.promotionalCodeList = promotionalCodeList;
	}

	public PromotionalCode getPromotionalCode() {
		return promotionalCode;
	}

	public void setPromotionalCode(PromotionalCode promotionalCode) {
		this.promotionalCode = promotionalCode;
	}
	public Map<Integer, PromotionalCode> getPromotionalCodeMap() {
		return promotionalCodeMap;
	}
	public void setPromotionalCodeMap(
			Map<Integer, PromotionalCode> promotionalCodeMap) {
		this.promotionalCodeMap = promotionalCodeMap;
	}
}
